package com.yanqu.road.server.cmd.voucher;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.voucher.eVoucherType;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.voucher.VoucherProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_VOUCHER_USE, desc = "使用代金券")
public class VoucherUseCmd extends AbstractCommand {

    private int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        VoucherProto.VoucherUseReqMsg reqMsg = VoucherProto.VoucherUseReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 判断下开关
        if (ChannelConfig.VOUCHER_EXCHANGE_SWITCH == 0) {
            return;
        }
        // 需要预扣除的道具
        Property cost = new Property();
        Map<Integer, List<BigInteger>> priceParamMap = new HashMap<>();
        if (type == eVoucherType.MeiTuan.getValue()) {
            // 查一下背包的道具
            // 代金券列表
            List<GoodsInfo> infos = GoodsMgr.getGoodsListByType(eGoodsType.MeiTuanQuan.getValue());
            if (infos == null || infos.size() == 0) {
                return;
            }
            for (GoodsInfo goodsInfo : infos) {
                UserBagItem userBagItem = player.getModule(BagModule.class).getUserBagItem(goodsInfo.getGoodsId());
                if (userBagItem == null) {
                    continue;
                }
                if (userBagItem.getNum() > 0) {
                    cost.addProperty(new Property(goodsInfo.getGoodsId(), userBagItem.getNum()));
                    priceParamMap.put(goodsInfo.getGoodsId(), goodsInfo.getParamList());
                }
            }
        } else {
            return;
        }
        // 检测道具是否足够
        if (cost.isNothing() || !player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            return;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(cost);

        VoucherProto.ChannelVoucherUseReqMsg.Builder reqMsg = VoucherProto.ChannelVoucherUseReqMsg.newBuilder();
        reqMsg.setChannelId(ServerListMgr.getChannelId(GameServer.getInstance().getServerId()));
        reqMsg.setServerId(GameServer.getInstance().getServerId());
        reqMsg.setType(type);
        reqMsg.setCost(PropertyHelper.parsePropertyToString(cost));
        reqMsg.setPriceParam(JSON.toJSONString(priceParamMap));
        // 上跨域
        player.sendChannelPlayerPacket(ChannelProtocol.N_CROSS_VOUCHER_USE, reqMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
